home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 5 / MacMania 5.toast / / Tools&Utilities / Plotfoil 3.2 / ui.c < prev   
Encoding:
C/C++ Source or Header  |  1995-06-09  |  5.0 KB  |  217 lines  |  [TEXT/MMCC]

  1.  
  2. /*
  3.  * Does prompting for plotfoil.
  4.  */
  5.  
  6. #include "plotfoil.h"
  7. #include "externs.h"
  8.  
  9. static char *banner = "Welcome to the interactive interface to plotfoil.\n\n";
  10.  
  11. static void get_str(char *, char *);
  12. static int get_pct(char *);
  13. static char get_char(char *, char *);
  14. static void read_value(char *prompt, char units, value_t *v);
  15. static float get_float(char *prompt);
  16.  
  17. void prompt_mode()
  18. {
  19.    char ans, raw_units;
  20.    int raw_off;
  21.    char outfile[TXTSIZ], infile[TXTSIZ];
  22.    FILE *f_in;
  23.    airfoil_data_t info;
  24.  
  25.    fputs(copyright, stdout);
  26.    fputs(banner, stdout);
  27.  
  28.    for(;;) {
  29.       get_str("Type the name of the airfoil data file: ", infile);
  30.       if(!(f_in = fopen(infile, "r")))
  31.      printf("Couldn't open \"%s\" for reading!\n", infile);
  32.       else
  33.      break;
  34.    }
  35.  
  36.    for(;;) {
  37.       get_str("Type a filename to save the output in: ", outfile);
  38.       if(!(fout = fopen(outfile, "w")))
  39.      printf("Couldn't open \"%s\" for writing!\n", outfile);
  40.       else
  41.      break;
  42.    }
  43.    
  44.    raw_units = get_char("What units will you be using? ", "mic");
  45.    read_value("What chord would you like to plot? ", raw_units, &chord);
  46.  
  47.    ans = get_char("Do you want to allow for sheeting thickness?", "yn");
  48.    if(ans == 'y') {
  49.       iflag++;
  50.       read_value("What is the thickness of the sheeting? ", raw_units,
  51.          &sheeting);
  52.    }
  53.    
  54.    ans = get_char("Do you want a rectangular template outline?", "yn");
  55.    if(ans == 'y') {
  56.       tflag++;
  57.       read_value("Template height above and below the mean chord line: ",
  58.         raw_units, &templ_h);
  59.       read_value("How far in front of the LE should it extend? ", raw_units,
  60.         &templ_le);
  61.       read_value("How far behind the TE should it extend? ", raw_units,
  62.         &templ_te);
  63.    }
  64.  
  65.    ans = get_char("Do you want to plot any spars?", "yn");
  66.    if(ans == 'y') {
  67.       do {
  68.      raw_off = get_pct("At what % chord is the spar? ");
  69.      spars[nspars].offset = raw_off/100.0;
  70.      read_value("What thickness is this spar? ", raw_units,
  71.             &spars[nspars].thick);
  72.      nspars++;
  73.      ans = get_char("Another spar?", "yn");
  74.       } while(ans == 'y');
  75.    }
  76.  
  77.    ans = get_char("Do you want to slant the plot?", "yn");
  78.    if(ans == 'y') {
  79.       angle = get_float("Enter angle in degrees (- clockwise, + ccw) ");
  80.       aflag++;
  81.    }
  82.  
  83.    ans = get_char("Do you need to reposition the plot on the paper?", "yn");
  84.    if(ans == 'y') {
  85.       read_value("Move to the right by: ", raw_units, &xfudge);
  86.       read_value("Move up by: ", raw_units, &yfudge);
  87.    }
  88.    
  89.    /*
  90.    ans = get_char("Do you want to change the straight line default? ", "ny");
  91.    if(ans == 'y') {
  92.       sflag++;
  93.       s_limit = get_float("Plot straight lines for x greater than: ");
  94.    }
  95.    */
  96.  
  97.    if(!(read_foil(f_in, infile, &info)))
  98.       return;
  99.    plot(&info);
  100. }
  101.  
  102. static void read_value(char *prompt, char units, value_t *v)
  103. {
  104.    char buffer[TXTSIZ], this_unit, *p;
  105.    
  106.    for(;;) {
  107.       fputs(prompt, stdout);
  108.       fflush(stdout);
  109.       fgets(buffer, TXTSIZ, stdin);
  110.       if((p = strchr(buffer, '\n')) != 0)
  111.      *p = 0;
  112.       p = buffer;
  113.       if(*p == '-') 
  114.      p++;
  115.       if(!isdigit(*p)) {
  116.      fputs("Numeric argument required.\n", stdout);
  117.      continue;
  118.       }
  119.       while(isdigit(*p) || (*p == '.'))
  120.      p++;
  121.       this_unit = *p;
  122.       if(!(this_unit == 0 || strchr("mic", this_unit))) {
  123.      printf("Unknown unit suffix `%c'.\n", this_unit);
  124.      continue;
  125.       }
  126.       *p = 0;
  127.       if(sscanf(buffer, "%f", &v->raw_value) == 1)
  128.      break;
  129.    }
  130.    if(!this_unit)
  131.       this_unit = units;
  132.    switch((int)this_unit) {
  133.    case 'i':
  134.       v->scale = 72.0; v->units = "\""; break;
  135.    case 'c':
  136.       v->scale = 28.34; v->units = "cm"; break;
  137.    case 'm':
  138.    default:
  139.       v->units = "mm"; v->scale = 2.834; break;
  140.    }
  141.    v->pts = v->raw_value * v->scale;
  142. }
  143.  
  144. static int get_pct(char *s)
  145. {
  146.    char buffer[TXTSIZ];
  147.    char *p;
  148.    int value;
  149.    
  150.    for(;;) {
  151.       fputs(s, stdout);
  152.       fflush(stdout);
  153.       fgets(buffer, TXTSIZ, stdin);
  154.       if((p = strchr(buffer, '\n')) != 0)
  155.      *p = 0;
  156.       p = buffer;
  157.       while(*p && isdigit(*p))
  158.      p++;
  159.       if(*p || (value = atoi(buffer)) > 100) {
  160.      fputs("Need a number between 0 and 100, please!\n", stdout);
  161.      continue;
  162.       }
  163.       return value;
  164.    }
  165. }
  166.  
  167. static float get_float(char *prompt)
  168. {
  169.    char buffer[TXTSIZ];
  170.    char *p;
  171.    float value;
  172.    int sign = 1;
  173.    
  174.    fputs(prompt, stdout);
  175.    fflush(stdout);
  176.    fgets(buffer, TXTSIZ, stdin);
  177.    if((p = strchr(buffer, '\n')) != 0)
  178.       *p = 0;
  179.    p = buffer;
  180.    if(*p == '-') {
  181.       sign = -1;
  182.       p++;
  183.    }
  184.    if(*p == '+')
  185.       p++;
  186.    value = (float) atof(p);
  187.  
  188.    return sign * value;
  189. }
  190.  
  191. static void get_str(char *prompt, char *reply)
  192. {
  193.    char *p;
  194.    fputs(prompt, stdout);
  195.    fgets(reply, TXTSIZ, stdin);
  196.    if((p = strchr(reply, '\n')) != 0)
  197.       *p = 0;
  198. }
  199.  
  200.  
  201. static char get_char(char *prompt, char *valid)
  202. {
  203.    char buffer[TXTSIZ], *p;
  204.  
  205.    for(;;) {
  206.       printf("%s [%s] ", prompt, valid);
  207.       fgets(buffer, TXTSIZ, stdin);
  208.       if((p = strchr(buffer, '\n')) != 0)
  209.      *p = 0;
  210.       if(!buffer[0])
  211.      return valid[0];
  212.       if(strchr(valid, buffer[0]))
  213.      return buffer[0];
  214.       printf("Valid replies are <%s>!\n", valid);
  215.    }
  216. }
  217.